package petertel.pbx.connector;

import java.util.HashMap;
import java.util.Map;

public class ConnectionFactory {

	private static final ConnectionFactory instance;
	static {
		instance = new ConnectionFactory();
	}

	private ConnectionFactory() {

	}

	public static ConnectionFactory getInstance() {
		return instance;
	}

	private Map<String, Connection> map = new HashMap<String, Connection>();

	public IConnection openConnection(String username, String password,
			String hostname) {
		final IConnection conn = getConnection(username, password, hostname);
		return new IConnection() {

			public IResult execute(String command) {
				return conn.execute(command);
			}

			public void disconnect() {
				;
			}
		};
	}

	private IConnection getConnection(String username, String password,
			String hostname) {
		String key = username + ":" + password + "@" + hostname;
		Connection conn = map.get(key);
		if (conn == null) {
			conn = new Connection();
			conn.connect(username, password, hostname);
			map.put(key, conn);
		}
		return conn;
	}

}
